package com.sc.everyonerentboot.service.Impl;

import com.sc.everyonerentboot.mapper.BrowsingHistoryMapper;
import com.sc.everyonerentboot.mapper.ProductsMapper;
import com.sc.everyonerentboot.pojo.BrowsingHistoryKey;
import com.sc.everyonerentboot.pojo.Products;
import com.sc.everyonerentboot.service.BrowseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrowseServiceImpl implements BrowseService {
    @Autowired
    ProductsMapper productsMapper;
    @Autowired
    BrowsingHistoryMapper browsingHistoryMapper;

    @Override
    public List<Products> showHistory(Integer userId) {
        Integer[] pids = browsingHistoryMapper.selectPidByUserId(userId);
        if (pids.length == 0){
            return null;
        }
        return productsMapper.selectByProductIds(pids);
    }

    @Override
    public int delBroHistory(Integer userId, Integer[] pids) {
        return browsingHistoryMapper.deleteByUserIdAndPids(userId, pids);
    }

    @Override
    public int addBroHistory(Integer userId, Integer pid) {
        int result = browsingHistoryMapper.findByUserIdAndPid(userId,pid);
        if (result == 0) {
            return browsingHistoryMapper.addBrowsingHistory(userId, pid);
        }
        return 0;
    }
}
